
from celery_task.main import celery_app
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

MY_QQEMAIL = '471043129@qq.com'  # 发件人邮箱账号
MY_SECRET = 'jmtsiurfuippcadg'  # 发件人邮箱密码
MY_163EMAIL = '15678376507@163.com'


@celery_app.task(name='send_email')
def send_mail(follower, content, to_email=MY_163EMAIL):
    ret = True
    try:
        html_msg = '<p>你醒辣！</p>' \
                   '<p>%s %s。</p>' \
                   '<p>快去看看吧。</p>' % (follower, content)
        msg = MIMEText(html_msg, 'html', 'utf-8')
        msg['From'] = formataddr(["<pekora微信公众号>", MY_QQEMAIL])  # 括号里的对应发件人邮箱昵称、发件人邮箱账号
        msg['To'] = formataddr(["我自己", to_email])  # 括号里的对应收件人邮箱昵称、收件人邮箱账号
        msg['Subject'] = "新的关注者！！！"  # 邮件的主题，也可以说是标题

        server = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 发件人邮箱中的SMTP服务器，端口是25
        server.login(MY_QQEMAIL, MY_SECRET)  # 括号中对应的是发件人邮箱账号、邮箱密码
        server.sendmail(MY_QQEMAIL, [to_email, ], msg.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.quit()  # 关闭连接
    except Exception as e:  # 如果 try 中的语句没有执行，则会执行下面的 ret=False
        print('邮件发送失败:', e)
        ret = False
    return ret


# if __name__ == '__main__':
#     follower = '你自己'
#     send_mail(follower)



